package edu.ynu.e_platform.controllers;

import com.alibaba.fastjson2.reader.ObjectReader;
import edu.ynu.e_platform.common.controller.LogicController;
import edu.ynu.e_platform.dao.CustomerDao;
import edu.ynu.e_platform.dto.OrderDTO;
import edu.ynu.e_platform.dto.ShopItemDTO;
import edu.ynu.e_platform.entities.Customer;
import edu.ynu.e_platform.entities.Order;
import edu.ynu.e_platform.service.CustomerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author manwen Yang
 */
@Slf4j
@Tag(name = "顾客 实体控制器")
@RestController
@RequestMapping("/customer")
public class CustomerController
        extends LogicController<CustomerService, CustomerDao, Customer, Long> {

    @Resource
    ModelMapper modelMapper;

    public CustomerController(@Autowired CustomerService ls) {
        super(ls);
    }

    @Operation(summary = "注册顾客")
    @GetMapping("/enroll")
    public Customer enroll(String customerName, String password, Integer sex){

        return getService().enroll(customerName, password, sex);
    }

    /**
     * 顾客挑选到的应该是店铺上架了的，而店铺上架的商品为shopItem
     */
    @Operation(summary = "顾客 挑选商品")
    @GetMapping("/pickItem")
    public OrderDTO pickItem(Long customerId, Long shopItemId, Double quantity){

        var result = getService().pickItem(customerId, shopItemId, quantity);
        var dtoResult = modelMapper.map(result, OrderDTO.class);

        return dtoResult;
    }

    @Operation(summary = "顾客 支付订单")
    @GetMapping("/payOrder")
    public OrderDTO payOrder(Long customerId, Long orderId){

        var result = getService().payment(customerId, orderId);
        var dtoResult = modelMapper.map(result, OrderDTO.class);

        return dtoResult;
    }

    @Operation(summary = "顾客 取消订单")
    @GetMapping("/cancelOrder")
    public Boolean cancelOrder(Long customerId, Long orderId){

        return getService().cancelOrder(customerId, orderId);
    }

    @Operation(summary = "顾客 查询未支付订单")
    @GetMapping("/queryUnpay")
    public List<OrderDTO> queryUnpay(Long customerId){

        var results = getService().queryOrderUnpay(customerId);

        return results.stream().map(
                order -> modelMapper.map(order, OrderDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "顾客 查询已支付订单")
    @GetMapping("/queryPay")
    public List<OrderDTO> queryPay(Long customerId){

        var results = getService().queryOrderPay(customerId);

        return results.stream().map(
                order -> modelMapper.map(order, OrderDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "顾客 查询所有订单")
    @GetMapping("/queryOrders")
    public List<OrderDTO> queryOrders(Long customerId){

        List<Order> results = getService().queryOrders(customerId);
        return results.stream().map(
                order -> modelMapper.map(order, OrderDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "顾客 分页 查询未支付订单")
    @GetMapping("/pageQueryUnpay")
    public List<OrderDTO> pageQueryUnpay(Integer pageStart, Integer pageSize, Long customerId){

        var results = getService().pageQueryOrderUnpay(pageStart, pageSize, customerId);

        return results.stream().map(
                order -> modelMapper.map(order, OrderDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "顾客 分页 查询已支付订单")
    @GetMapping("/pageQueryPay")
    public List<OrderDTO> pageQueryPay(Integer pageStart, Integer pageSize, Long customerId){

        var results = getService().pageQueryOrderPay(pageStart, pageSize, customerId);

        return results.stream().map(
                order -> modelMapper.map(order, OrderDTO.class)
        ).collect(Collectors.toList());
    }
}
